package com.appbee.gdata.youtube.client.ui;

import com.appbee.gdata.youtube.client.model.SimpleEntry;
import com.appbee.gdata.youtube.client.search.Entry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

public class EmbeddedPlayer extends Widget
{
	private String width = "425";
	private String height = "355";
	private String src = "";
	private boolean allowScriptAccess = true;
	private boolean autoplay = false;
	
	interface Template extends SafeHtmlTemplates
	{
		@Template("<object width='{0}' height='{1}'>{2}</object>")
		SafeHtml obj(String width,String height,SafeHtml content);
		
		@Template("<param name='{0}' value='{1}'></param>")
		SafeHtml param(String name,String value);
		
		@Template("<embed type='application/x-shockwave-flash' src='{0}' width='{1}' height='{2}' allowscriptaccess='{3}' autoplay='{4}'></embed>")
		SafeHtml embed(String src,String widht,String height,String allowScrtiptAccess,String autoplay);
	}
	private static final Template t = GWT.create(Template.class);

	public EmbeddedPlayer()
	{
		setElement(DOM.createElement("div"));
	}

	@Override
	protected void onAttach()
	{
		super.onAttach();
		render();
	}

	private void render()
	{
		if(src!=null && src.length()>0)
		{
			SafeHtmlBuilder sb = new SafeHtmlBuilder();
			
			sb.append(t.param("movie", src));
			sb.append(t.param("allowScriptAccess", allowScriptAccess?"1":"0"));
			sb.append(t.param("autoplay", autoplay?"1":"0"));
			
			sb.append(t.embed(src, width, height, allowScriptAccess?"1":"0", autoplay?"1":"0"));
			
			getElement().setInnerHTML(t.obj(width, height,sb.toSafeHtml()).asString());
		}
		else
		{
			getElement().setInnerHTML("");
		}
	}

	public void setSize(String width,String height)
	{
		this.width = width;
		this.height = height;
		if(isAttached())
			render();
	}
	
	public String getWidth()
	{
		return width;
	}

	public void setWidth(String width)
	{
		this.width = width;
	}

	public String getHeight()
	{
		return height;
	}

	public void setHeight(String height)
	{
		this.height = height;
	}

	public String getSrc()
	{
		return src;
	}

	public void setSrc(String src)
	{
		this.src = src;
		if(isAttached())
			render();
	}

	public boolean isAllowScriptAccess()
	{
		return allowScriptAccess;
	}

	public void setAllowScriptAccess(boolean allowScriptAccess)
	{
		this.allowScriptAccess = allowScriptAccess;
	}

	public boolean isAutoplay()
	{
		return autoplay;
	}

	public void setAutoplay(boolean autoplay)
	{
		this.autoplay = autoplay;
		if(isAttached())
			render();
	}
	
	public void setEntry(Entry entry)
	{
		String id = entry.getId();
		String videoId = id.substring(id.indexOf("video:")+6);
		setSrc("http://www.youtube.com/v/"+videoId);
		
	}
	public void setSimpleEntry(SimpleEntry entry)
	{
		String id = entry.getId();
		String videoId = id.substring(id.indexOf("video:")+6);
		setSrc("http://www.youtube.com/v/"+videoId);
		
	}


}
